package directory.controller;

import controller.*;
import java.sql.ResultSet;
import java.util.Vector;
import directory.model.Person;

public class PersonMgr extends DbManager {

    public static final PersonMgr mgr = new PersonMgr();

    private PersonMgr() {
        super("Person");
        m_titles = new String[]{"NIT", "NOMBRE", "DIRECCION", "TELEFONO", "CELULAR", "EMAIL", "MUNICIPIO"};
    }

    @Override
    protected Person getBean() {
        return new Person();
    }

    @Override
    public Person getItem(String id) {
        return (Person) super.getItem(id);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void addObject(Vector v, ResultSet rs) {
        v.add(new Person(rs));
    }

    public static Vector<Person> search(String nit, String name) {
        return mgr.executeQuery("SELECT * from Person where nombre like '%" + name + "%' and nit like '" + nit + "%' order by nombre");
    }

    public static Vector<Person> search(String nit, String name, String idTipo, String idRelacion, String idMunicipio, String idDepto) {

        String comp = "";

        if (idTipo != null && !idTipo.equals("--------------") && !idTipo.equals("")) {
            comp = " and idPersonType=" + idTipo + " ";
        }

        if (!idRelacion.equals("--------------") && !idRelacion.equals("")) {
            comp = " and id in ( select idPersonB from PersonRelationship where idPersonRelationType=" + idRelacion + " ) ";
        }

        if (idDepto != null && !idDepto.equals("--------------") && !idDepto.equals("")) {
            String comp2 = " and idMunicipio like '" + idDepto + "%' ";

            if (idMunicipio != null && !idMunicipio.equals("--------------") && !idMunicipio.equals("")) {
                comp2 = " and idMunicipio = '" + idMunicipio + "' ";
            }
            comp += comp2;
        }

        return mgr.executeQuery("SELECT * from Person where nombre like '%" + name + "%' and nit like '" + nit + "%' " +
                " " + comp + " order by nombre");
    }

    public static Vector<Person> getRelatedPersons(String idPerson, String idRelationType) {
        return mgr.executeQuery("select * from Person where id in ( select idPersonB " +
                "from PersonRelationship where idPersonA='" + idPerson + "' and idPersonRelationType='" + idRelationType + "') order by nombre");
    }

    public static Vector<Person> getListFromMunicipio(String idType, String idMpio) {
        return mgr.executeQuery("SELECT * from Person where idPersonType ='" + idType + "' and idMunicipio = '" + idMpio + "' order by nombre");
    }

    public static Vector<Person> getListFromDepartamento(String idDepto) {
        return mgr.executeQuery("SELECT * from Person where idMunicipio like'" + idDepto + "%' order by idMunicipio");
    }

    public static Vector<Person> getListFromDepto(String idType, String idDepto) {
        return mgr.executeQuery("SELECT * from Person where idPersonType ='" + idType + "' and idMunicipio like '" + idDepto + "%' order by nombre");
    }

}